package com.zdp.netty.chapter12.server;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;

/**
 * @Author zhengdp
 * @Description UDP协议 无服务端/客户端概念，双方平等
 */
public class ChineseProverbServer {

    public void run(int port) throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioDatagramChannel.class) // UDP通信
                    .option(ChannelOption.SO_BROADCAST, true) // 支持广播
                    // 因为 UDP不存在 客户端与服务端的实际连接，所以不需要为连接（ChannelPipeline设置handler）
                    .handler(new ChineseProverbServerHandler());
            b.bind(port).sync().channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        new ChineseProverbServer().run(8080);
    }

}
